/*
 * Copyright TLR Team, Inc. All Rights Reserved.
 */
package com.tfb.rong;

import com.tfb.service.utility.Utils;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.NumberUtils;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;

/**
 *
 * @author james
 */
public class CustomJsonDateTimeDeserializer extends JsonDeserializer<Date> {

    private static final Logger log = Logger.getLogger(CustomJsonDateTimeDeserializer.class);

    @Override
    public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = jp.getText();
        try {
            return NumberUtils.isNumber(date) ? new Date(Long.parseLong(date)) : format.parse(date);
        } catch (ParseException e) {
            log.error(Utils.getFullException(e));
            throw new RuntimeException(e);
        }
    }
}
